<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 顺子牌 -->
    <script>
      var isNStraightHand = function (hand, groupSize) {
        const n = hand.length
        if (n % groupSize !== 0) {
          return false
        }
        hand.sort((a, b) => a - b)
        const cnt = new Map()
        for (const x of hand) {
          cnt.set(x, (cnt.get(x) || 0) + 1)
        }
        for (const x of hand) {
          if (!cnt.has(x)) {
            continue
          }
          for (let j = 0; j < groupSize; j++) {
            const num = x + j
            if (!cnt.has(num)) {
              return false
            }
            cnt.set(num, cnt.get(num) - 1)
            if (cnt.get(num) == 0) {
              cnt.delete(num)
            }
          }
        }
        return true
      }
      console.log(isNStraightHand([1, 2, 3, 6, 2, 3, 4, 7, 8], 3))
    </script>
  </body>
</html>
